import "@/assets/style/index.scss";
import "@arco-design/web-vue/dist/arco.css";
import ArcoVue from "@arco-design/web-vue/es/arco-vue";
import ArcoVueIcon from "@arco-design/web-vue/es/icon";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import "uno.css";
import { renderRouters } from "./api/service/common";
import { setProfile } from "./api/service/user/utils";
import App from "./App.vue";
import directives from "./directives/index";
import { getLangList } from "./i18n";
import router from "./router/index";
import { getToken, setToken } from "./store/modules/user/utils";
import { getStoreConfig } from "./views/admin/system/system";

const app = createApp(App);

const bootstrap = async () => {
  await getLangList();

  const pinia = createPinia();

  pinia.use(piniaPluginPersistedstate);
  app.use(pinia);

  await renderRouters();
  app.use(router);
  app.use(directives);
  app.use(ArcoVue);
  app.use(ArcoVueIcon);
  const token = getToken();
  /* 我希望把 location的参数传给下面的router.push */
  const url = window.location.href.split("#")[1] || "";
  if (!token) {
    if (url?.includes("token")) {
      const [, auth = ""] = url.split("=");
      setToken(auth);
      await renderRouters();
      await setProfile();
      window.location.href = "/#/home";
    } else {
      router.push({ name: "home" });
    }
  } else {
    getStoreConfig();
  }
  app.mount("#app");
};
bootstrap();
